<template>
	<view :data-theme="theme" class="container">
		<view class="header">
			<uni-notice-bar color="#ff3c0b" background-color="#fff0f0" :text="noticeText" />
		</view>
		<view class="content" style="border-radius: 10rpx;margin: 20rpx;">
			<!-- 这里是可滚动的内容 -->
			<scroll-view class="scroll-view" scroll-y>
				<!-- 内容区域 -->
				<view class="scroll-view-inner" style="border-radius: 10px;background-color: #fff;"
					v-for="(item,indexO) in contactData">
					<h2>{{ item.contacts }}</h2>
					<view class="stockOrder">
						<view class="businessStock">
							<view class="top_right" style="padding-left: 20rpx;">
								<view style="margin-bottom: 10rpx;" class="mb_10">负责城市： <span>{{ item.city }}</span>
								</view>
								<view style="margin-bottom: 10rpx;" class="mb_10">联系人： <span>{{ item.name }}</span>
								</view>
								<view style="margin-bottom: 10rpx;" class="mb_10">联系电话：
									<span>{{ item.telephone }}</span>
								</view>
								<view style="margin-bottom: 10rpx;" class="mb_10">联系邮箱： <span>{{ item.email }}</span>
								</view>
							</view>
							<view style="width: 50px;">
								<button type="primary" @click="deleteContact(item)">删除</button>
								<button type="primary" @click="goChooseProduct('edit',item,indexO)">修改</button>
							</view>
						</view>
						<view style="margin:20rpx;border-bottom: 1px solid #f6f6f6"></view>
					</view>
				</view>
				<view class="scroll-view-inner" style="border-radius: 10px;background-color: #fff;"
					v-for="(ele,index) in addContactData">
					<!-- 内容区域 -->
					<h2>{{ ele.contacts }}</h2>
					<view class="stockOrder">
						<view class="businessStock">
							<view class="top_right" style="padding-left: 20rpx;">
								<!-- <view style="margin-bottom: 10rpx;" class="top_right_weight mb_10" >{{ ele.contacts }}</view> -->
								<view style="margin-bottom: 10rpx;" class="mb_10">负责城市： <span>{{ ele.city }}</span>
								</view>
								<view style="margin-bottom: 10rpx;" class="mb_10">联系人： <span>{{ ele.name }}</span>
								</view>
								<view style="margin-bottom: 10rpx;" class="mb_10">联系电话： <span>{{ ele.telephone }}</span>
								</view>
								<view style="margin-bottom: 10rpx;" class="mb_10">联系邮箱： <span>{{ ele.email }}</span>
								</view>
							</view>
							<view style="width: 50px;">
								<button type="primary" @click="deleteAddCancel(index)">删除</button>
							</view>
						</view>
						<view style="margin:20rpx;border-bottom: 1px solid #f6f6f6"></view>
					</view>
				</view>
				<view style="margin:0 20rpx 20rpx 20rpx;flex: 1;">
					<!-- <u-button type="primary" :color="bgColor" :plain="true" text="+ 新增联系人"
						@click="goChooseProduct('add')"></u-button> -->
					<button :color="bgColor" class="plain_bgColor"
				style="font-weight: bold;border-radius: 16rpx;color: #FFF;padding: 20rpx;"
						@click="goChooseProduct('add')">+ 新增联系人</button>
				</view>
			</scroll-view>
		</view>


		<view class="footer">
			<view style="margin:0 20rpx 20rpx 20rpx;">
				<!-- <u-button :color="bgColor" text="更新保存"  style="color: #fff;"
					@click="saveContacts()"></u-button> -->
				<button :color="bgColor" class="bg_color"
				style="font-weight: bold;border-radius: 16rpx;color: #FFF;padding: 20rpx;"
					@click="saveContacts()">更新保存</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		saveContactsApi,
		getContactsApi,
		deleteContactsApi,
		updateContactsApi
	} from '@/api/user.js';
	import {
		setThemeColor
	} from '@/utils/setTheme.js'
	let app = getApp();
	export default {
		data() {
			return {
				theme: app.globalData.theme,
				noticeText: '业务负责人仅主账号可修改且联系电话变更后会导致您主账号同步发生变更，请谨慎修改。',
				contactData: [],
				addContactData: [],
			};
		},
		onLoad() {
			this.bgColor = setThemeColor();
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.bgColor,
			});
			this.getContactsList();
		},
		methods: {
			getContactsList() {
				getContactsApi().then((res) => {
					this.contactData = res.data
				})
			},
			// 删除
			deleteContact(item) {
				let ids = [];
				ids.push(item.id)
				deleteContactsApi(ids).then((res) => {

				})
			},
			// 编辑联系人信息
			goChooseProduct(type, item, index) {
				let data = JSON.stringify(item)
				this.goPage(`/pages/contactInformation/addContacts/addContacts?type=${type}`,
					(res) => {
						res.eventChannel.emit('contactsAttr', {
							// 通过eventCannel 向北打开页面传送数据
							index: index,
							data: item,
							type: type,
							from: 'saveContact',
						})
					}
				);
			},
			goPage(url, success) {
				uni.navigateTo({
					url,
					success: success
				})
			},
			otherFun(contactAttrParam) {
				// this.addContactData.push(items);
				if (contactAttrParam) {
					//添加编辑规格参数
					if (contactAttrParam.type == "add") { //添加
						this.contactData.push(contactAttrParam.data);
					} else { //编辑
						this.contactData.splice(contactAttrParam.index, 1, contactAttrParam.data)
					}
				}
			},
			// 页面删除
			deleteAddCancel(index) {
				this.addContactData.splice(index, 1) //使用splice 方法删除当前对象
			},
			// 添加常用联系人
			saveContacts() {
				// 更新联系人
				let isUpdate = parseFloat(this.id) > 0;
				console.log(isUpdate)
				console.log(this.contactData);
				let contactVaule = [];
				for(const item of this.contactData){
					contactVaule.push({
						city:item.city,
						contacts:item.contacts,
						email:item.email,
						id:item.id,
						name:item.name,
						telephone:parseInt(item.telephone),
					})
				};
				let api = isUpdate ? saveContactsApi : updateContactsApi;
				api(contactVaule).then((res) => {
					console.log(res)
				})
			},
		}
	}
</script>

<style lang="scss">
	.bg_color {
		@include main_bg_color(theme);
	}
	
	.plain_bgColor {
		@include main_color(theme);
		@include main_bg_rgba_color(theme, 0.05);
		border: 1rpx solid;
	}
	

	h1 {
		font-weight: bold;
		font-size: 20px;
		margin-bottom: 20px;
	}

	h2 {
		font-weight: bold;
		margin: 20px;
		padding-top: 20rpx;
	}

	page {
		height: 100%;
	}

	.container {
		display: flex;
		flex-direction: column;
		height: 100%;
	}

	.businessStock {
		display: flex;
		flex-flow: row;
		padding: 20rpx;

		.top_right {
			margin-left: 20rpx;
		}
	}

	// .header {
	// 	// height: 100px;
	// 	// background-color: #fff;
	// }

	.content {
		flex: 1;
		overflow: hidden;
	}

	.scroll-view {
		height: 100%;
		padding-bottom: 180rpx;
	}

	.footer {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: #fff;
		padding: 20rpx 0;
		// display: flex;
		justify-content: space-between;
		text-align: center;
		justify-items: center;
	}
</style>